package com.yuliang.ai.service.Impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yuliang.ai.domain.Dto.ScenicCategoryDto;
import com.yuliang.ai.domain.Dto.ScenicDetailWithSkusAndPriceDto;
import com.yuliang.ai.domain.Dto.ScenicWithFilterDto;
import com.yuliang.ai.domain.Dto.TourCommonDto;
import com.yuliang.ai.domain.Vo.ScenicDetailWithSkusAndPriceVo;
import com.yuliang.ai.domain.Vo.ScenicWithFilterVo;
import com.yuliang.ai.service.ScenicService;
import com.yuliang.utils.result.ResultUtils;
import com.yuliang.utils.result.emums.HttpCodeEnum;
import com.yuliang.utils.util.HttpRequestUtils;
import com.yuliang.utils.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Huangkai
 */
@Slf4j
@Service
public class ScenicServiceImpl implements ScenicService {

    @Value("${spring.requestUrl}")
    private String requestUrl;

    @Override
    public String selectScenicList() {
        String requestPath = requestUrl + "/app/json/ela_search/getScenicSearchParams";
        JSONObject jsonObject = new JSONObject();
        Map<String, String> header = new HashMap<>(2);
        header.put("referrer", "c:app::0EDDDEEF167C465A8DAD5CD36C5A1C73");
        String result = HttpRequestUtils.doPost(requestPath, header, jsonObject.toJSONString());
        JSONObject resultObject = JSONObject.parseObject(result);
        Integer status = resultObject.getInteger("status");
        if(status == 0){
            JSONObject object = resultObject.getJSONObject("data");
            ScenicCategoryDto scenicCategoryDto = object.toJavaObject(ScenicCategoryDto.class);
            log.info("酒店星级数据{}", scenicCategoryDto);
            return ResultUtils.ResultWithMessageDataEnum(HttpCodeEnum.select_success, scenicCategoryDto);
        }
        return ResultUtils.ResultWithMessageEnum(HttpCodeEnum.data_not_exist);
    }

    @Override
    public TourCommonDto selectScenicWithFilter(ScenicWithFilterVo scenicWithFilterVo) {
        try{
            String requestPath = requestUrl + "/app/json/ela_search/getScenicsWithFilter";
            Map<String, Object> mapParam = new HashMap<>(4);
            //城市非空
            if (StringUtils.isNotEmpty(scenicWithFilterVo.getCity())) {
                mapParam.put("city", scenicWithFilterVo.getCity());
            }
            //城市非空
            if (StringUtils.isNotEmpty(scenicWithFilterVo.getKeyWord())) {
                mapParam.put("keyWord", scenicWithFilterVo.getKeyWord());
            }
            //城市非空
            if (StringUtils.isNotEmpty(scenicWithFilterVo.getLevel())) {
                mapParam.put("level", scenicWithFilterVo.getLevel());
            }
            //城市非空
            if (StringUtils.isNotEmpty(scenicWithFilterVo.getResCategory())) {
                mapParam.put("resCategory", scenicWithFilterVo.getResCategory());
            }
            mapParam.put("searchPage", scenicWithFilterVo.getSearchPage());
            mapParam.put("searchSize", scenicWithFilterVo.getSearchSize());
            Map<String, String> header = new HashMap<>(2);
            header.put("referrer", "c:app::0EDDDEEF167C465A8DAD5CD36C5A1C73");
            String result = HttpRequestUtils.doPost(requestPath, header, mapParam);
            JSONObject resultObject = JSONObject.parseObject(result);
            Integer status = resultObject.getInteger("status");
            log.info("查询的地址{}", resultObject);
            if(status == 0){
                JSONObject jsonObject = resultObject.getJSONObject("data");
                log.info("景区景点搜索{}", jsonObject);
                ScenicWithFilterDto scenicWithFilterDto = jsonObject.toJavaObject(ScenicWithFilterDto.class);
                TourCommonDto tourCommonDto = new TourCommonDto();
                tourCommonDto.setIndexName(scenicWithFilterDto.getIndexName());
                tourCommonDto.setHotelPage(null);

                List<ScenicWithFilterDto.ScenicElaSearchModel> scenicWithFilterDtoList = scenicWithFilterDto.getScenicElaSearchModel();
                if(scenicWithFilterDtoList != null && scenicWithFilterDtoList.size() > 0){
                    List<TourCommonDto.ElaSearchListModel> elaSearchListModels = new ArrayList<>();
                    scenicWithFilterDtoList.forEach(scenicElaSearchModel -> {
                        TourCommonDto.ElaSearchListModel elaSearchListModel = new TourCommonDto.ElaSearchListModel();
                        elaSearchListModel.setHotelName(scenicElaSearchModel.getScenicName());
                        elaSearchListModel.setShowPrice(scenicElaSearchModel.getSalePrice());
                        elaSearchListModel.setCoverPic(scenicElaSearchModel.getCoverPic());
                        elaSearchListModels.add(elaSearchListModel);
                        List<ScenicWithFilterDto.JumpLink> jumpLinkList = scenicElaSearchModel.getJumpLinks();
                        ScenicWithFilterDto.JumpLink jumpLink = jumpLinkList.get(1);
                        String url = jumpLink.getUrl();
                        try {
                            url = url + "&sCode=SJZY&sName=" + URLEncoder.encode("三姐助游", "UTF-8");
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                        jumpLink.setUrl(url);
                        List<TourCommonDto.JumpLink> tourCommonJumpLinkList = new ArrayList<>();
                        jumpLinkList.forEach(jumpLink1 -> {
                            TourCommonDto.JumpLink TourCommonJumpLink = new TourCommonDto.JumpLink();
                            TourCommonJumpLink.setAppId(jumpLink1.getAppId());
                            TourCommonJumpLink.setMode(jumpLink1.getMode());
                            TourCommonJumpLink.setAppName(jumpLink1.getAppName());
                            TourCommonJumpLink.setUrl(jumpLink1.getUrl());
                            tourCommonJumpLinkList.add(TourCommonJumpLink);
                        });
                        elaSearchListModel.setJumpLinks(tourCommonJumpLinkList);
                    });
                    tourCommonDto.setElaSearchListModels(elaSearchListModels);
                    return tourCommonDto;
                }
                return null;
            }
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
        return null;
    }

    @Override
    public String getScenicDetailWithSkusAndPrice(ScenicDetailWithSkusAndPriceVo scenicDetailWithSkusAndPriceVo) {
        String url = requestUrl + "/app/json/ela_search/getScenicDetailWithSkusAndPrice";
        JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(scenicDetailWithSkusAndPriceVo));
        Map<String, String> header = new HashMap<>(2);
        header.put("referrer", "c:app::0EDDDEEF167C465A8DAD5CD36C5A1C73");
        String result = HttpRequestUtils.doPost(url, header, jsonObject.toJSONString());
        JSONObject resultObject = JSONObject.parseObject(result);
        Integer status = resultObject.getInteger("status");
        if(status == 0){
            JSONObject object = resultObject.getJSONObject("data");
            log.info("景区景点搜索{}", object);
            ScenicDetailWithSkusAndPriceDto scenicDetailWithSkusAndPriceDto = object.toJavaObject(ScenicDetailWithSkusAndPriceDto.class);
            log.info("值{}", JSONObject.toJSONString(scenicDetailWithSkusAndPriceDto));
            return ResultUtils.ResultWithMessageDataEnum(HttpCodeEnum.select_success, scenicDetailWithSkusAndPriceDto);
        }
        return ResultUtils.ResultWithMessageEnum(HttpCodeEnum.data_not_exist);
    }
}
